﻿using AnesSystem.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.Services
{
    class PatientDrugsService :BaseService<PatientDrugsService, PatientDrugsModel>
    { 
        public void addUpdate(PatientDrugsModel patientDrugsModel)
        {
            //HelperManager.Instance.MongoDB.Insert<D>(tObj, ClassName);
            if (patientDrugsModel.Id == new ObjectId("000000000000000000000000"))
            {
                Insert(patientDrugsModel);
            }
            else {
                Update(patientDrugsModel);
            }
           
        }

        // 通过时间查找
        public List<PatientDrugsModel> GetPatientDrugs8Time(String pid)
        {  //start,end:查找时间区间

            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Eq("PatientID", pid),
                filter.Ne("Status", "invalid")
                );
            //Builders<BsonDocument>.Filter.Gte("BeginTime", start),
            //Builders<BsonDocument>.Filter.Lte("BeginTime", end)
            return FindAll(query).OrderBy(O => O.BeginTime).ToList();
        }
        // 通过时间查找
        public List<PatientDrugsModel> GetPatientDrugs8Pid(String pid)
        {  //start,end:查找时间区间

            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(
                filter.Eq("PatientID", pid)
                );
            //Builders<BsonDocument>.Filter.Gte("BeginTime", start),
            //Builders<BsonDocument>.Filter.Lte("BeginTime", end)
            return FindAll(query).OrderBy(O => O.BeginTime).ToList();
        }
        public bool DelOneExe(PatientDrugsModel PatientDrugs)
        {
            long rs= DeleteById(PatientDrugs.Id);
            if (rs > 0)
            {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 获取单个时间点之后的所有药物
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public List<PatientDrugsModel> GetPatientDrugs8tTime(DateTime time)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Gte("BeginTime", time));
            return FindAll(query).ToList();
        }
        }
    }
